rve<template>
	<el-form ref="loginForm" :model="form" :rules="rules" label-width="0" size="large">
		<el-form-item prop="user">
			<el-input v-model="form.user" prefix-icon="el-icon-user" clearable :placeholder="$t('login.userPlaceholder')">
				
			</el-input>
		</el-form-item>
		<el-form-item prop="password">
			<el-input v-model="form.password" prefix-icon="el-icon-lock" clearable show-password :placeholder="$t('login.PWPlaceholder')"></el-input>
		</el-form-item>
		
		<el-form-item>
			<el-button type="primary" style="width: 100%;" :loading="islogin" round @click="login">{{ $t('login.signIn') }}</el-button>
		</el-form-item>
		<el-form-item style="margin-bottom: 10px;">
				<el-col :span="12">
				<div class="login-reg">
			{{$t('login.noAccount')}} <router-link to="/user_register">{{$t('login.createAccount')}}</router-link>
		</div>
					
				</el-col>
				<el-col :span="12" class="login-forgot">
					<router-link to="/reset_password">{{ $t('login.forgetPassword') }}？</router-link>
				</el-col>
		</el-form-item>
		
	</el-form>
</template>

<script>
export default {
  data() {
    return {
      userType: "admin",
      form: {
        user: "admin",
        password: "123456",
        autologin: false,
      },
      rules: {
        user: [{ required: true, message: this.$t("login.userError"), trigger: "blur" }],
        password: [
          { required: true, message: this.$t("login.PWError"), trigger: "blur" },
        ],
      },
      islogin: false,
    };
  },
  watch: {
    userType(val) {
      if (val == "admin") {
        this.form.user = "admin";
        this.form.password = "123456";
      } else if (val == "user") {
        this.form.user = "user";
        this.form.password = "user";
      }
    },
  },
  mounted() {},
  methods: {
    async login() {
      var validate = await this.$refs.loginForm.validate().catch(() => {});
      if (!validate) {
        return false;
      }
      //let old_token = this.$TOOL.cookie.get("TOKEN");
      //let old_deviecode = this.$TOOL.cookie.get("DEVICECODE");

      this.islogin = true;
      //随机32位设备码，当前时间戳+6位随机整数，MD5加密（32位）
      let time = parseInt(new Date().getTime() / 1000) + '';
			let random = Math.floor(Math.random() * (999999 - 100000) ) + 100000;
      var deviceCode = this.$TOOL.crypto.MD5(time+random);
      //写入设备码
      this.$TOOL.cookie.set("DEVICECODE", deviceCode, {
          expires: 24 * 60 * 60 ,
        });
       var data = {
         username: this.form.user,
         password: this.form.password,//this.$TOOL.crypto.MD5(this.form.password),
         deviceCode: deviceCode
       };
      var user = await this.$API.courseware.login.post(data);
      if(user.status == 200){
					this.$TOOL.cookie.set("TOKEN", user.data, {
						expires: this.form.autologin? 24*60*60 : 0
					});

          var userinfo = await this.$API.courseware.userinfo.get();
          this.$TOOL.data.set("USER_INFO", {
            userId: userinfo.data.userId,
            loginAccount: userinfo.data.username,
            userName: userinfo.data.name,
            dashboard: "0",
            role: userinfo.data.roleStrings,
          });
					
				}else{
					this.islogin = false
					this.$message.warning(user.message)
					return false
				}
        
			//this.$TOOL.cookie.set("TOKEN", 'SCUI.Administrator.Auth', {
      //    expires: this.form.autologin ? 24 * 60 * 60 : 0,
      //  });
      //准备取消
      

      
      //获取菜单
      var menu = {
        code: 200,
        data: {
          menu: [],
          permissions: [
            "list.add",
            "list.edit",
            "list.delete",
            "user.add",
            "user.edit",
            "user.delete",
          ],
          dashboardGrid: ["welcome", "ver", "time", "progress", "echarts", "about"],
        },
        message: "",
      };
      
      if (menu.code == 200) {
        // if(menu.data.menu.length==0){
        // 	this.islogin = false
        // 	this.$alert("当前用户无任何菜单权限，请联系系统管理员", "无权限访问", {
        // 		type: 'error',
        // 		center: true
        // 	})
        // 	return false
        // }
        this.$TOOL.data.set("MENU", menu.data.menu);
        this.$TOOL.data.set("PERMISSIONS", menu.data.permissions);
      } else {
        this.islogin = false;
        this.$message.warning(menu.message);
        return false;
      }

      this.$router.replace({
        path: "/",
      });
      this.$message.success("登录成功");
      this.islogin = false;
    },
  },
};
</script>

<style></style>
